﻿using Jack.EtcdConfiguration.WebApiDemo.Options;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;

namespace Jack.EtcdConfiguration.WebApiDemo.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ConfigsController : ControllerBase
    {
        [HttpGet]
        public RedisOptions GetRedisOptions([FromServices] IOptions<RedisOptions> options)
        {
            return options.Value;
        }

        [HttpGet]
        public RedisOptions GetRedisOptionsMonitor([FromServices] IOptionsMonitor<RedisOptions> optionsMonitor)
        {
            return optionsMonitor.CurrentValue;
        }

        [HttpGet]
        public RedisOptions GetRedisOptionsSnapshot([FromServices] IOptionsSnapshot<RedisOptions> optionsSnapshot)
        {
            return optionsSnapshot.Value;
        }

        [HttpGet]
        public EfCoreOptions GetEfCoreOptions([FromServices] IOptions<EfCoreOptions> options)
        {
            return options.Value;
        }

        [HttpGet]
        public EfCoreOptions GetEfCoreOptionsMonitor([FromServices] IOptionsMonitor<EfCoreOptions> optionsMonitor)
        {
            
            return optionsMonitor.CurrentValue;
        }

        [HttpGet]
        public EfCoreOptions GetEfCoreOptionsSnapshot([FromServices] IOptionsSnapshot<EfCoreOptions> optionsSnapshot)
        {
            return optionsSnapshot.Value;
        }

        [HttpGet]
        public AppSettingOptions GetAppSettingOptions([FromServices] IOptions<AppSettingOptions> options)
        {
            return options.Value;
        }

        [HttpGet]
        public AppSettingOptions GetAppSettingOptionsMonitor([FromServices] IOptionsMonitor<AppSettingOptions> optionsMonitor)
        {

            return optionsMonitor.CurrentValue;
        }

        [HttpGet]
        public AppSettingOptions GetAppSettingOptionsSnapshot([FromServices] IOptionsSnapshot<AppSettingOptions> optionsSnapshot)
        {
            return optionsSnapshot.Value;
        }

        [HttpGet]
        public ActionResult GetHostApplicationLifetime([FromServices] IHostApplicationLifetime hostApplicationLifetime)
        {
            return Ok(new
            {
                ApplicationStarted = hostApplicationLifetime.ApplicationStarted.IsCancellationRequested,
                ApplicationStopping = hostApplicationLifetime.ApplicationStopping.IsCancellationRequested,
                ApplicationStopped = hostApplicationLifetime.ApplicationStopped.IsCancellationRequested,
            });
        }

        [HttpGet]
        public string GetConfigurationValue([FromServices] IConfiguration configuration)
        {
            return configuration["EfCoreOptions:ConnectionString"];
        }
    }
}
